package ui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.RGBImageFilter;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import engine.Sector;
import engine.stars.DwarfStar;
import engine.stars.GiantStar;
import engine.stars.RegularStar;

/**
 * Класс для отображения звезд на звездной карте
 * 
 * @author Anton Nesterov
 */
public class VisualSector extends JPanel {

	private static final long serialVersionUID = 1L;
	Sector starmap;
	private Image star;
	private Image giantStar;

	public VisualSector(Sector starmap) {
		this.starmap = starmap;
		/* Сетка */
		setBorder(BorderFactory.createLineBorder(Color.BLUE));

		try {
			star = ImageIO.read(new File("image/star.png"));
			giantStar = ImageIO.read(new File("image/star_big.png"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		/*
		 * TODO Вплывающая подсказка на панели сектора. Не работает.
		 */
		if (starmap.getStar() != null) {
			setToolTipText(starmap.getStar().getLocalisedName());
		}

	}

	/* Прорисовка звезды на панели */
	public void paintComponent(Graphics g) {
		if (starmap.getStar() != null) {
			if (starmap.getStar() instanceof RegularStar) {

				ImageFilter filter = new GetFilter(starmap.getStar().getColor());
				FilteredImageSource filterSrc = new FilteredImageSource(star.getSource(),
					filter);
				star = Toolkit.getDefaultToolkit().createImage(filterSrc);
				g.drawImage(star, (getWidth() / 2) - getHeight() / 2, (getHeight() / 2)
					- getHeight() / 2, getHeight(), getHeight(), null);

			}
			if (starmap.getStar() instanceof GiantStar) {

				ImageFilter filter = new GetFilter(starmap.getStar().getColor());
				FilteredImageSource filterSrc = new FilteredImageSource(giantStar.getSource(),
					filter);
				giantStar = Toolkit.getDefaultToolkit().createImage(filterSrc);
				g.drawImage(giantStar, (getWidth() / 2) - getHeight() / 2, (getHeight() / 2)
					- getHeight() / 2, getHeight(), getHeight(), null);
			}
			if (starmap.getStar() instanceof DwarfStar) {
				g.drawImage(star, (getWidth() / 2) - getHeight() / 6, (getHeight() / 2)
					- getHeight() / 6, getHeight() / 3, getHeight() / 3, null);
			}
		}

	}

}

// This filter removes all but the color values in an image
class GetFilter extends RGBImageFilter {
	int color;

	public GetFilter(int color) {

		this.color = color;
		// When this is set to true, the filter will work with images
		// whose pixels are indices into a color table (IndexColorModel).
		// In such a case, the color values in the color table are filtered.
		canFilterIndexColorModel = true;
	}

	// This method is called for every pixel in the image
	public int filterRGB(int x, int y, int rgb) {
		if (x == -1) {
			// The pixel value is from the image's color table rather than the
			// image itself
		}
		// Return only color component
		return rgb & color;
	}
}
